<?php 
$tpl=new TemplatePower("skin/sk_shurtcut.htm");
$tpl->prepare();
//---------- config
$manager="QU&#7842;N L&#221; TRUY C&#7852;P NHANH";// tieu de trang

$par_page		="sr_shortcut";// vi tri trang hien thi trong site
$item           ="shortcut";
$id_item        ="id_shortcut";

$tpl->assignGlobal("manager",$manager);
$tpl->assignGlobal("par_page",$par_page);

$id=intval($_GET['id']);
$tpl->assignGlobal("id",$id);
if($_GET['code']=='00' || !$_GET['code']){
	showlist();
}
if($_GET['code']=='01'){
	$tpl->newBlock("addnew");
	$tpl->assign("thu_tu",ordering('shortcut'));
	showlist();
}
if($_GET['code']=='02'){
	$in_title=compile_post('title');
	if($in_title){
		$a=array(
					'title'=>$in_title
				);
		$a['link']=compile_post('link');
		if($_FILES['image']['size']){
			$dispath=$CONFIG['root_path']."admin/imageshort/";
			$image=uploadimage('image',$thumnail='no',$dispath);
		}		
		$a['image']=$image['image'];
		$a['thu_tu']=intval(compile_post('thu_tu'));
		$a['active']=intval(compile_post('active'));
		$b=$DB->compile_db_insert_string($a);
		$sql="INSERT INTO ".$item." (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
		$DB->query($sql);
		message("Th&#234;m m&#7899;i th&#224;nh c&#244;ng !");
	}else {
		message("Kh&#244;ng &#273;&#7911; d&#7919; li&#7879;u &#273;&#7847;u v&#224;o !");
	}
	showlist();
}
if($_GET['code']=='03'){
	$tpl->newBlock("update");
	$sql="SELECT * FROM ".$item." WHERE ".$id_item."=".$id;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$tpl->assign("title",$rs['title']);
		$tpl->assign("link",$rs['link']);
		$tpl->assign("thu_tu",$rs['thu_tu']);
		if($rs['active']==1){
			$tpl->assign("active","checked");
		}else {
			$tpl->assign("active","");
		}
		$image="";
		$image="<img src='imageshort/".$rs['image']."' width='48' ><br>";
		$tpl->assign("image",$image);
	}
	showlist();
}
if($_GET['code']=='04'){
	$in_title=compile_post('title');
	if($in_title){
		$a=array(
					'title'=>$in_title
				);
		$a['link']=compile_post('link');
		
		if($_FILES['image']['size']){
			$sql="SELECT * FROM ".$item." WHERE ".$id_item."=".$id;
			$db=$DB->query($sql);
			if($rs=mysql_fetch_array($db)){
				$lastfile=$rs['image'];
			}
			$dispath=$CONFIG['root_path']."admin/imageshort/";
			deleteimage($lastfile,'','',$dispath);
			$image=uploadimage('image',$thumnail='no',$dispath);
		}		
		$a['image']=$image['image'];
		$a['thu_tu']=intval(compile_post('thu_tu'));
		$a['active']=intval(compile_post('active'));
		$b=$DB->compile_db_update_string($a);
		$sql="UPDATE ".$item." SET ".$b." WHERE ".$id_item."=".$id;
		$DB->query($sql);
		message("S&#7917;a ch&#7919;a th&#224;nh c&#244;ng !");
		
	}else {
		message("Kh&#244;ng &#273;&#7911; d&#7919; li&#7879;u &#273;&#7847;u v&#224;o !");
	}
	showlist();
}
if($_GET['code']=='05'){
	$sql="SELECT * FROM ".$item." WHERE ".$id_item."=".$id;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$lastfile=$rs['image'];
	}
	$dispath=$CONFIG['root_path']."admin/imageshort/";
	deleteimage($lastfile,'','',$dispath);
	$sql="DELETE FROM ".$item." WHERE ".$id_item."=".$id;
	$DB->query($sql);
	message("&#272;&#227; x&#243;a xong !");
	showlist();
}
if($_GET['code']=='06'){
	$sql="SELECT * FROM ".$item." ORDER BY thu_tu DESC, title ASC";
	$db=$DB->query($sql);
	change_active_order_paging($item,$id_item,$db);
	showlist();
}

function showlist(){
	global $DB,$tpl,$item,$id_item;
	$sql="SELECT * FROM ".$item." ORDER BY thu_tu DESC, title ASC";
	$db=$DB->query($sql);
	$tt=0;
	$i=0;
	while($rs=mysql_fetch_array($db)){
		$tt++;
		$tpl->newBlock("list1");
		$tpl->assign("title",$rs['title']);
		$tpl->assign("link",$rs['link']);
		$tpl->assign("thu_tu",$rs['thu_tu']);
		$tpl->assign("id",$rs[$id_item]);
		if($rs['active']==1){
			$tpl->assign("active","checked");
			$tpl->assign("style","");
		}else {
			$tpl->assign("active","");
			$tpl->assign("style","msg");
		}
		$tpl->assign("stt",$tt);
		$image="";
		$image="<img src='imageshort/".$rs['image']."' width='48' border='0'>";
	
		$tpl->assign("image",$image);
		$i++;
		if($i==2){
			$tpl->assign("cl","bg_row_list");
			$i=0;
		}else {
			$tpl->assign("cl","");
		}
	}
}
$tpl->printToScreen();
?>